package com.dfrz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dfrz.entity.Video;
import com.dfrz.entity.VideoDetail;
import com.dfrz.mapper.IVideoMapper;
import com.dfrz.mapper.VideoDetailMapper;
import com.dfrz.service.VideoDetailService;
import com.dfrz.utils.AliOSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 29877
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class VideoDetailServiceImpl implements VideoDetailService {
    Random random = new Random();
    @Resource
    private VideoDetailMapper videoDetailMapper;

    @Resource
    private IVideoMapper videoMapper;

    @Override
    public int insertAll(VideoDetail videoDetail, MultipartFile detailImg, MultipartFile detailVideo) throws Exception {
        String imgUrl = AliOSSUtil.uploadFile(detailImg);
        String detailUrl = AliOSSUtil.upLoadPartFile(detailVideo);
        videoDetail.setDetailImg(imgUrl);
        videoDetail.setDetailUrl(detailUrl);
        return videoDetailMapper.insert(videoDetail);
    }

//    排除未通过审核的剧集
    @Override
    public List<VideoDetail> selectdetialbyvid(Integer vid) {
        List<VideoDetail> videoDetails=videoDetailMapper.selectVideoByVid(vid);
        Iterator<VideoDetail> iterator = videoDetails.iterator();
        while (iterator.hasNext()){
            if(iterator.next().getCheckStatus()!=1){
                iterator.remove();
            }
        }
        return videoDetails;
    }

    @Override
    public List<VideoDetail> selectdetialbytitle(String word) {
        word="%"+word+"%";
        return videoDetailMapper.selectDetailByTitle(word);
    }

    @Override
    public VideoDetail getdetailinfobyid(Integer vid) {
        return videoDetailMapper.getDetailInfoById(vid);
    }
    //首页随机获取视频
    @Override
    public List<Object> getVideoByRandom() {
        //统计总共有多少条博主视频
        QueryWrapper<VideoDetail> detail = new QueryWrapper<VideoDetail>()
                .eq("detail_type",0)
                .eq("check_status", 1);
        long numDetial =  videoDetailMapper.selectCount(detail);
        long num =  numDetial/10;
        int page = 1;
        if (num != 0) {
            page =random.nextInt(Math.toIntExact(num))+1;
        }
        List<Object> indexVideo = videoDetailMapper.getVideoByRandom((page-1)*10,10);

        //统计有多少条非博主视频
        QueryWrapper<VideoDetail> detailM = new QueryWrapper<VideoDetail>()
                .eq("detail_type",1)
                .eq("check_status", 1);
        long numDetialM =  videoDetailMapper.selectCount(detailM);
        int pageM =random.nextInt(Math.toIntExact(numDetialM))+1;
        List<Object> objects = videoDetailMapper.getVideoByRandomM((pageM-1),1);
        indexVideo.addAll(objects);
        return indexVideo;
    }
    //滚动刷新视频
    @Override
    public List<Object> getVideoByRandomM() {
        //统计总共有多少条博主视频
        QueryWrapper<VideoDetail> detail = new QueryWrapper<VideoDetail>()
                .eq("detail_type",0)
                .eq("check_status", 1);
        long numDetial =  videoDetailMapper.selectCount(detail);
        long num = numDetial/4;
        int page = 1;
        if (num != 0) {
            page =random.nextInt(Math.toIntExact(num))+1;
        }
        List<Object> indexVideo = videoDetailMapper.getVideoByRandom((page-1)*4,4);

        //统计有多少条非博主视频
        QueryWrapper<VideoDetail> detailM = new QueryWrapper<VideoDetail>()
                .eq("detail_type",1)
                .eq("check_status", 1);
        long numDetialM =  videoDetailMapper.selectCount(detailM);
        int pageM =random.nextInt(Math.toIntExact(numDetialM))+1;
        List<Object> objects = videoDetailMapper.getVideoByRandomM((pageM-1),1);
        indexVideo.addAll(objects);
        return indexVideo;
    }

//    我的动态视频删除
    @Override
    public void deleteDetailByVideoId(Integer videoId) {
        QueryWrapper<Video> videoId1 = new QueryWrapper<Video>()
                .eq("video_id", videoId);
        videoMapper.delete(videoId1);
    }


    @Override
    public List<Object> getMyVideo(Integer userId, String videoTitle) {
        videoTitle ="%"+videoTitle+"%";
        return  videoDetailMapper.getMyVideo(userId,videoTitle);
    }

    @Override
    public Map<String, Object> getCheckVideo() {
        QueryWrapper<VideoDetail> eq = new QueryWrapper<VideoDetail>()
                .eq("detail_type", 0)
                .eq("check_status", 0);
        Long countPage = videoDetailMapper.selectCount(eq)/10+1;
        List<Object> videos = videoDetailMapper.getVideoDetailAll(0,10);
        Map<String,Object> map =new HashMap<>();
        map.put("countPage", countPage);
        map.put("checkList", videos);
        return map;
    }

    @Override
    public void updataCheck(Integer videoId) {
        QueryWrapper<VideoDetail> videoUid = new QueryWrapper<VideoDetail>()
                .eq("video_uid", videoId);
        VideoDetail videoDetail = new VideoDetail();
        videoDetail.setCheckStatus(1);
        videoDetailMapper.update(videoDetail, videoUid);
    }

    @Override
    public List<Object> getPageing(Integer page) {
        page=(page-1)*10;
        return videoDetailMapper.getVideoDetailAll(page, 10);
    }

    @Override
    public Map<String, Object> selectvideoByName(String videoTitle) {
        QueryWrapper<VideoDetail> eq = new QueryWrapper<VideoDetail>()
                .eq("detail_type", 0)
                .eq("check_status", 0);
        Long countPage = videoDetailMapper.selectCount(eq)/10+1;
        List<Object> videos = videoDetailMapper.getVideoDetailAllSearch(0,10,videoTitle);
        Map<String,Object> map =new HashMap<>();
        map.put("countPage", countPage);
        map.put("checkList", videos);
        return map;
    }
}
